package integral

import (
	"context"
	"fmt"
	"strings"

	"mall/common/modelx"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/user/model"
	"mall/service/user/rpc/internal/svc"
	"mall/service/user/rpc/types/user"

	"github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserTransactionLogListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserTransactionLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserTransactionLogListLogic {
	return &GetUserTransactionLogListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserTransactionLogListLogic) GetUserTransactionLogList(in *user.BaseListReq) (*user.UserTransactionLogListResp, error) {
	logx.Infof("GetIntegralTransactionLogList : %+v ", in)
	if in.Filter == nil || len(*in.Filter) == 0 {
		return nil, resultx.StatusError(resultx.REUQEST_PARAM_ERROR, "")
	}
	preloadMap, eagerLoadMap := modelx.ParsePreloadAndEagerLoad(in.Preload, in.EagerLoad)

	logrus.Info(fmt.Sprintf("GetIntegralTransactionLogList preloadMap: %+v", preloadMap))

	logrus.Info(fmt.Sprintf("GetIntegralTransactionLogList eagerLoadMap: %+v", eagerLoadMap))

	userIntegralTransactionEntityName := model.RpcEntityNames.UserTransactionLog
	_, hasuserMap := model.RpcEntityPreloadMap[userIntegralTransactionEntityName]
	if !hasuserMap {
		return &user.UserTransactionLogListResp{}, nil
	}

	userIntegralTransactionRepositoryName := model.RepositoryNames.UserTransactionLog
	columnMap, hasColumnMap := model.RepositoryPreloadMap[userIntegralTransactionRepositoryName]
	if !hasColumnMap {
		return &user.UserTransactionLogListResp{}, nil
	}

	var selectCols []string
	if preloadFields, hasPreloadFields := preloadMap[userIntegralTransactionEntityName]; hasPreloadFields {
		selectCols = modelx.ParseSelectColumns(preloadFields, columnMap)
	}
	filterMap := modelx.ParseFilter(in.Filter, columnMap)
	logrus.Info(fmt.Sprintf("GetIntegralTransactionLogList filterMap: %+v", filterMap))
	if filterMap == nil {
		return nil, resultx.StatusError(resultx.REUQEST_PARAM_ERROR, "")
	}
	orderBy := modelx.ParseSorter(in.Sorter, columnMap)
	logrus.Info(fmt.Sprintf("GetIntegralTransactionLogList orderBy: %s", orderBy))
	if len(orderBy) == 0 {
		orderBy = model.UserTaskLogColumns.UpdatedAt + " DESC"
	}

	res, count, err := l.svcCtx.CurrentUser.GetTransactionLogList(l.ctx, model.M{
		"offset": in.Offset,
		"limit":  in.Limit,
		// "search":  in.Search,
		"orderBy": orderBy,
		"select":  strings.Join(selectCols, ","),
	}, 2, filterMap)
	if err != nil {
		return nil, err
	}

	hasNextPage, hasPreviousPage := modelx.ParseCount(count)
	resp := &user.UserTransactionLogListResp{
		HasNextPage:     &hasNextPage,
		HasPreviousPage: &hasPreviousPage,
	}

	if len(res) > 0 {
		for _, v := range res {
			resp.List = append(resp.List, &user.UserTransactionLogInfo{
				Id:           pointy.GetPointer(v.UserTransactionLogID),
				UserId:       pointy.GetPointer(v.UserID),
				UserName:     pointy.GetPointer(v.UserName),
				AssetType:    pointy.GetPointer(uint32(v.AssetType)),
				ChangeType:   pointy.GetPointer(uint32(v.ChangeType)),
				ChangeAmount: pointy.GetPointer(v.ChangeAmount),
				OldAmount:    pointy.GetPointer(v.OldAmount),
				SourceType:   pointy.GetPointer(uint32(v.SourceType)),
				SourceId:     pointy.GetPointer(v.SourceID),
				Desc:         pointy.GetPointer(v.Desc),
				Status:       pointy.GetPointer(uint32(v.Status)),
				CreatedAt:    pointy.GetPointer(modelx.FormatTime(v.CreatedAt, "")),
				UpdatedAt:    pointy.GetPointer(modelx.FormatTime(v.UpdatedAt, "")),
			})
		}
	}
	resp.Total = uint64(len(resp.List))

	return resp, nil
}
